package com.gd.coreservice.controller;


import com.gd.commonutils.Constants;
import com.gd.commonutils.Result;
import com.gd.coreservice.entity.Goods;
import com.gd.coreservice.service.GoodsService;
import com.gd.servicebase.exceptionhandler.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-03-23
 */
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    //查询  已测试
    @GetMapping("/getGoods")
    public List<Goods> getGoods() {
        return goodsService.findAll();
    }

    //根据uid查询数据 卖家id  如果有商品名称或者staus就会作为like条件进行模糊查询
    //注意 一定三个参数都要有
    @GetMapping("/getUidGoods")
    public Map<String, Object> getUidGoods(@RequestParam("uid") String uid, @RequestParam("status") String status, @RequestParam("name") String name) {

        List<Goods> data = goodsService.getUidGoods(uid,status,name);
        Integer total = data.size();
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    //根据商品id查询数据  已测试
    @GetMapping("/getOneGoods")
    public Goods findOne(@RequestParam("gid") String gid){
        return goodsService.findOne(gid);
    }

    //增加  已测试
    @PostMapping("/insertGoods")
    public Result addGoods(@RequestBody Goods goods) {
        if(goodsService.insertGoods(goods))
        {
            return Result.success("添加成功");
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }

    }

    //修改商品状态（学生用户修改商品状态）  已测试
    @PutMapping("/updateGoodsStatus")
    public Result updateGoodsStatus(@RequestParam("gid") String gid,@RequestParam("status") String status){
        if(goodsService.updateGoodsGstatus(gid,status))
        {
            return Result.success("交易成功");
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }
    }


    //管理员修改商品的信息  ？  未测试
    @PutMapping("/updateGoods")
    public Result updateGoods(@RequestBody Goods goods){
        if(goods.getIsApproved().equals("0"))
        {
            goods.setStatus("0");
        }
        else if(goods.getIsApproved().equals("1"))
        {
            goods.setStatus("1");
        }
        if(goodsService.updateGoods(goods))
        {
            return Result.success("修改成功");
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }
    }
    //学生用户修改  和前面两个方法一模一样
    @PutMapping("/updateStudentGoods")
    public Result updateStudentGoods(@RequestBody Goods goods){
        if(goodsService.updateStudentGoods(goods))
        {
            return Result.success("修改成功");
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }
    }

    //根据商品id删除商品  已测试
    @DeleteMapping("/deleteGoods/{Gid}")
    public Integer deleteGoods(@PathVariable("Gid") String  Gid){
        return goodsService.deleteGoods(Gid);
    }
    //批量删除  已测试 [1771850558722181111,1773989860390412290]
    @PostMapping("/deleteGoods/batch")
    public Integer deleteGoodsBatch(@RequestBody List<String> Gids){
        return goodsService.deleteGoodsBatch(Gids);
    }

    //学生分页查询   比起下面多了的条件 is_approved = 1 已测试
    @GetMapping("/getGoodsPage")
    public Map<String, Object> getGoodsPage(@RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize,
                                            @RequestParam String Gname) {
        Map<String,Object> resultmap = goodsService.getPageMap(pageNum, pageSize, Gname);
        List<Goods> data = (List<Goods>)resultmap.get("data");
        Long total =(Long)resultmap.get("total");
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    //管理员分页查询  比起上面的查询 少了个is_approved=1的条件 已测试
    @GetMapping("/getAdminGoodsPage")
    public Map<String, Object> getAdminGoodsPage(@RequestParam Integer pageNum,
                                                 @RequestParam Integer pageSize,
                                                 @RequestParam String Gname) {
        Map<String,Object> resultmap = goodsService.getAdminPageMap(pageNum, pageSize, Gname);
        List<Goods> data = (List<Goods>)resultmap.get("data");
        Long total =(Long) resultmap.get("total");
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    //查询总商品数  已测试
    @GetMapping("/getGoodsAllTotal")
    public int getAllTotal(){
        return goodsService.getAllTotal();
    }



}

